package com.zby.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.zby.business.R;
import com.zby.business.net.MyAsyncTask;
import com.zby.business.util.Util;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

/**
 * Created by BY on 2016/3/9.
 */
public class CartListViewAdapter extends BaseAdapter {
    private String userId;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private ArrayList commodityDetailId, commodityName, commodityPrice, commodityPicture, cartId;

    public CartListViewAdapter(Context context, String userId, ArrayList cartId, ArrayList commodityDetailId, ArrayList commodityPrice, ArrayList commodityName, ArrayList commodityPicture) {
        this.context = context;
        this.userId = userId;
        this.cartId = cartId;
        this.commodityDetailId = commodityDetailId;
        this.commodityName = commodityName;
        this.commodityPicture = commodityPicture;
        this.commodityPrice = commodityPrice;
        inflater = LayoutInflater.from(context);
        imageLoader = ImageLoader.getInstance();
        ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(context);
        imageLoader.init(config);
    }

    @Override
    public int getCount() {
        return commodityDetailId.size();
    }

    @Override
    public Object getItem(int position) {
        return commodityDetailId.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.cart_list_view, null);
        String[] urls = commodityPicture.get(position).toString().split("\\;");
        final ImageView carImage = (ImageView) view.findViewById(R.id.carListViewImage);
        TextView carIntroduce = (TextView) view.findViewById(R.id.carIntroduceTextView);
        TextView carprice = (TextView) view.findViewById(R.id.carPriceText);
        Button carCancel = (Button) view.findViewById(R.id.carCancelButton);
        Button carPay = (Button) view.findViewById(R.id.carPayButton);
        imageLoader.displayImage(Util.BASE_URL + urls[0], carImage);
        carIntroduce.setText(commodityName.get(position) + "");
        carprice.setText("￥" + commodityPrice.get(position));
        carCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyAsyncTask asyncTask = new MyAsyncTask();
                try {
                    String s = asyncTask.execute("deleteFromCart?userId=" + userId + "&detailId=" + commodityDetailId.get(position) + "&cartId=" + cartId.get(position)).get();
                    if (s.equals("success")) {
                        commodityDetailId.remove(position);
                        commodityPrice.remove(position);
                        commodityPicture.remove(position);
                        commodityName.remove(position);
                        cartId.remove(position);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        carPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return view;
    }
}
